<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: AddPortlet.php 367 2010-02-06 20:04:08Z steen.rabol@gmail.com $
 *
 */

class AddPortlet extends PortalAdminPage
{
	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->setAdminPageTitle('Add portlet');
	}

	public function saveButtonClicked($sender, $param)
	{
		if($this->IsValid)
		{
			// Load the portlet and perform 'installation'
			$classfile = $this->Application->BasePath . "/Portlets/" . $this->PortletName->Text . "/Install/" . $this->PortletName->Text. ".php";
			if(file_exists($classfile))
			{
				Prado::using('Application.Portlets.' . $this->PortletName->Text . '.Install.' . $this->PortletName->Text);

				$classname	= $this->PortletName->Text;
				if(class_exists($classname,false))
				{
					$portlet = new $classname;

					if($portlet->PortletInstall())
					{
						$name			= $this->PortletName->Text;
						$displayname	= $this->DisplayName->Text;
						$active			= TPropertyValue::ensureInteger($this->Active->Checked);
						$hasadmin		= TPropertyValue::ensureInteger($portlet->HasAdminFunc);
						$sql 			= "insert into tblportlets (name,displayname,active,hasadmin) values('$name','$displayname','$active','$hasadmin')";

						try
						{
							$this->DataAccess->createCommand($sql)->execute();
						}
						catch(Exception $e)
						{
							throw new PortalException(0,$e->getMessage());
						}

						$this->gotoPage('Admin.PortletMan');
					}
				}
				else
				{
					$this->reportError(0,Prado::localize('Portlet installation file {0} does not contain class {1}',array($classfile,$classname)));
				}
			}
			else
			{
				$this->reportError(0,Prado::localize('Portlet installation file {0} not found',array($classfile)));
			}
		}
	}

	public function validatePortletName($sender,$param)
	{
		try
		{
			$sql = "select id from tblportlets where name='" . $this->PortletName->Text . "'";
			$param->IsValid = !$this->DataAccess->createCommand($sql)->queryScalar();
		}
		catch(Execption $e)
		{
			$param->IsValid = false;
		}
	}

	public function validatePortletFile($sender,$param)
	{
		// Validate that the file is available
		$basePath		= dirname($this->getRequest()->getApplicationFilePath()). '/' . "protected/Portlets/" . $this->PortletName->Text . "/";
		$param->IsValid	= file_exists($basePath . $this->PortletName->Text . ".php");
	}
}
?>